package com.chandra.pos.web.purchase.action;

import com.chandra.pos.EntityNotFoundException;
import com.chandra.pos.dao.entity.*;
import com.chandra.pos.service.OrderManagement;
import com.chandra.pos.service.ProductService;
import com.chandra.pos.service.UserManagement;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.interceptor.SessionAware;

import java.math.BigDecimal;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * UserEntity: root
 * Date: 6/4/12
 * Time: 11:18 PM
 * To change this template use File | Settings | File Templates.
 */
public class PurchaseAction extends ActionSupport implements SessionAware {

    private UserManagement userManagementImpl;

    private ProductService productService;

    private OrderManagement orderManagement;

    private Map session;

    private OrderEntity orderEntity;


    public OrderManagement getOrderManagement() {
        return orderManagement;
    }

    public void setOrderManagement(OrderManagement orderManagement) {
        this.orderManagement = orderManagement;
    }

    public UserManagement getUserManagementImpl() {
        return userManagementImpl;
    }

    public void setUserManagementImpl(UserManagement userManagementImpl) {
        this.userManagementImpl = userManagementImpl;
    }

    public ProductService getProductService() {
        return productService;
    }

    public void setProductService(ProductService productService) {
        this.productService = productService;
    }

    public String doNew() {

        if (session.get("orderEntity") != null) {
            this.orderEntity = (OrderEntity) session.get("orderEntity");


        }
        return INPUT;
    }

    public String doSave() {

        return SUCCESS;
    }

    public String doEdit() {

        return SUCCESS;
    }

    public String doUpdate() {

        return SUCCESS;
    }

    public String doList() {


        return SUCCESS;
    }

    public String doUnSuspend() {


        return SUCCESS;
    }


    public String doAddItem() {

        OrderEntity orderEntity = (OrderEntity) session.get("orderEntity");

        try {
            ProductEntity product = productService.getProduct(itemId);

            OrderItemEntity item = new OrderItemEntity();
            item.setQuantity(new BigDecimal(10));
            item.setSalePrice(new BigDecimal(1000));
            item.setProduct(product);
            orderEntity.getItemList().add(item);

        } catch (EntityNotFoundException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }


        //  gets


        return SUCCESS;
    }

    public String doEditItem() {
        ProductEntity product = null;
        try {
            product = productService.getProduct(itemId);
        } catch (EntityNotFoundException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        if (session.get("orderEntity") == null) {
            OrderEntity orderEntity = new OrderEntity();
            session.put("orderEntity", orderEntity);

        }
        OrderEntity orderEntity = (OrderEntity) session.get("orderEntity");
        OrderItemEntity item = new OrderItemEntity();
        item.setQuantity(quantity);
        item.setSalePrice(price);
        item.setProduct(product);
        item.setDiscount(discount);
        orderEntity.getItemList().remove(item);
        orderEntity.getItemList().add(item);


        //  gets


        return SUCCESS;
    }

    public String doDeleteItem() {

        ProductEntity product = null;
        try {
            product = productService.getProduct(itemId);
        } catch (EntityNotFoundException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        OrderEntity orderEntity = (OrderEntity) session.get("orderEntity");
        OrderItemEntity item = new OrderItemEntity();
        item.setQuantity(new BigDecimal(10));
        item.setSalePrice(new BigDecimal(1000));
        item.setProduct(product);
        orderEntity.getItemList().add(item);


        return SUCCESS;
    }

    public String doAddShipping() {

        OrderEntity orderEntity = (OrderEntity) session.get("orderEntity");
        orderEntity.setShippingAmount(shippingAmount);
        return SUCCESS;
    }

    public String doAddPayment() {

        OrderEntity orderEntity = (OrderEntity) session.get("orderEntity");

        OrderPaymentEntity payment = new OrderPaymentEntity();
        payment.setAmount(paymentAmount);
        payment.setType(new PaymentTypeEntity(paymentType));
        orderEntity.getPaymentList().add(payment);
        return SUCCESS;
    }

    public String doDeletePayment() {

        return SUCCESS;
    }


    public String doAddCustomer() {
        CustomerEntity userEntity = null;
        try {
            userEntity = userManagementImpl.getCustomer(customerId);
        } catch (EntityNotFoundException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        OrderEntity orderEntity = (OrderEntity) session.get("orderEntity");

        orderEntity.setCustomer(userEntity);

        return SUCCESS;
    }

    public String doDeleteCustomer() {

        OrderEntity orderEntity = (OrderEntity) session.get("orderEntity");

        orderEntity.setCustomer(null);

        return SUCCESS;
    }

    public String doListSuspended() {

        return SUCCESS;
    }

    public String doSuspend() {

        return SUCCESS;
    }

    public String doCancel() {

        session.remove("orderEntity");
        return SUCCESS;
    }

    public String doComplete() {

        OrderEntity orderEntity = (OrderEntity) session.get("orderEntity");
        orderEntity.setNote(comment);

        // session.remove("orderEntity");
        orderManagement.createOrder(orderEntity);
        return SUCCESS;
    }

    public String doLoad() {

        OrderEntity orderEntity = (OrderEntity) session.get("orderEntity");
        orderEntity.setNote(comment);

        // session.remove("orderEntity");
        return SUCCESS;
    }


    private String comment;


    private long customerId;

    private long itemId;

    private BigDecimal price;

    private BigDecimal discount;

    private BigDecimal quantity;
    private BigDecimal shippingAmount;

    private BigDecimal paymentAmount;

    private long paymentType;

    public BigDecimal getPaymentAmount() {
        return paymentAmount;
    }

    public void setPaymentAmount(BigDecimal paymentAmount) {
        this.paymentAmount = paymentAmount;
    }

    public long getPaymentType() {
        return paymentType;
    }

    public void setPaymentType(long paymentType) {
        this.paymentType = paymentType;
    }

    public BigDecimal getPrice() {
        return price;
    }

    public void setPrice(BigDecimal price) {
        this.price = price;
    }

    public BigDecimal getDiscount() {
        return discount;
    }

    public void setDiscount(BigDecimal discount) {
        this.discount = discount;
    }

    public BigDecimal getQuantity() {
        return quantity;
    }

    public void setQuantity(BigDecimal quantity) {
        this.quantity = quantity;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public long getCustomerId() {
        return customerId;
    }

    public void setCustomerId(long customerId) {
        this.customerId = customerId;
    }

    public long getItemId() {
        return itemId;
    }

    public void setItemId(long itemId) {
        this.itemId = itemId;
    }

    @Override
    public void setSession(Map<String, Object> stringObjectMap) {
        this.session = stringObjectMap;
    }

    public OrderEntity getOrderEntity() {
        return orderEntity;
    }

    public void setOrderEntity(OrderEntity orderEntity) {
        this.orderEntity = orderEntity;
    }

    public BigDecimal getShippingAmount() {
        return shippingAmount;
    }

    public void setShippingAmount(BigDecimal shippingAmount) {
        this.shippingAmount = shippingAmount;
    }
}

